package adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.a17376080_dlz_finaltest.R;


import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import utils.HttpUtil;

public class MajorBcAdapter extends RecyclerView.Adapter<MajorBcAdapter.ViewHolder> {
    private static final int SUCCESS = 1;
    private List<MajorBc> mmajorBcList;
    private Context context;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                //加载网络成功进行UI的更新,处理得到的图片资源
                case SUCCESS:
                    Object[] objects= (Object[]) msg.obj;
                    ImageView imageView= (ImageView) objects[0];
                    if(imageView.getTag().equals(objects[2])){
                        byte[] Picture = (byte[]) objects[1];

                        //使用BitmapFactory工厂，把字节数组转化为bitmap
                        Bitmap bitmap = BitmapFactory.decodeByteArray(Picture, 0, Picture.length);
                        //通过imageview，设置图片
                        imageView.setImageBitmap(bitmap);
                    }
                    break;
                default:
                    break;


            }
        }
    };
    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView bc_view;
        public ViewHolder(View view){
            super(view);
            bc_view=view.findViewById(R.id.bc_view);
        }
    }
public MajorBcAdapter(List<MajorBc> majorBcList,Context context){
    this.mmajorBcList=majorBcList;
    this.context=context;
}
    @NonNull
    @Override
    public MajorBcAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.major_bc_item,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MajorBcAdapter.ViewHolder holder, int position) {
        MajorBc majorBc=mmajorBcList.get(position);
        holder.bc_view.setImageDrawable(context.getDrawable(R.mipmap.timg));
       holder.bc_view.setTag(majorBc.getImageurl());

       sendRequest(majorBc.getImageurl(),holder.bc_view);

    }

    private void sendRequest(final String imageurl, final ImageView bc_view) {
        HttpUtil.sendOkHttpRequest(imageurl, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("失败了", "onFailure: ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                byte[] Picture_bt = response.body().bytes();
                //通过handler更新UI
                Object[] objects=new Object[]{bc_view,Picture_bt,imageurl};
                Message message=handler.obtainMessage();
                message.obj=objects;
                message.what = SUCCESS;
                handler.sendMessage(message);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mmajorBcList.size();
    }


}
